package com.fc.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fc.dto.TableDTO;
import com.fc.service.CodeGenerateService;
import com.fc.service.SysPageSetService;
import com.fc.vo.ColumnVO;
import org.springframework.web.bind.annotation.RequestMapping;
import com.fc.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import com.fc.entity.SysTable;
import com.fc.service.SysTableService;


import org.springframework.stereotype.Controller;

import java.util.List;

/**
 * <p>
 * 代码生成表 前端控制器
 * </p>
 *
 * @author zffc
 * @since 2023-07-20
 */
@Controller
@RequestMapping("/sysTable")
public class SysTableController {


    @Autowired
    private SysPageSetService sysPageSetService;

    @Autowired
    private SysTableService sysTableService;

    @Autowired
    private CodeGenerateService codeGenerateService;

    @GetMapping("/toIndex")
    public ModelAndView index() {
        ModelAndView mv = new ModelAndView();
        //因为配置了自带的视图解析器，所以默认访问template目录
        mv.setViewName("/sysTable/list");
        return mv;
    }

    @GetMapping("/toAdd")
    public ModelAndView toAdd() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/sysTable/form");
        return mv;
    }

    @GetMapping("/toEdit/{id}")
    public ModelAndView toEdit(@PathVariable String id) {//路径参数
        ModelAndView mv = new ModelAndView();
        SysTable sysTable = sysTableService.getById(id);
        List<ColumnVO> columnList = codeGenerateService.getColumnByName(sysTable.getTableName());
        mv.setViewName("/sysTable/form");
        mv.addObject("sysTable", sysTable);
        mv.addObject("columnList",columnList);
        mv.addObject("query",sysPageSetService.getPageSetByTable(sysTable.getId(),1));
        mv.addObject("list",sysPageSetService.getPageSetByTable(sysTable.getId(),2));
        mv.addObject("form",sysPageSetService.getPageSetByTable(sysTable.getId(),3));

        return mv;
    }


    /**
     * @param sysTable
     * @param pageNumber
     * @param pageSize
     * @return
     */
    @GetMapping("/list")
    @ResponseBody
    public Result<?> list(SysTable sysTable,
                          @RequestParam(name = "pageNumber", defaultValue = "1") Integer pageNumber,
                          @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SysTable> page = new Page<SysTable>(pageNumber, pageSize);
        return Result.OK(sysTableService.page(page));
    }

    /**
     * 添加或修改方法
     *
     * @param sysTable
     * @return
     */
    @PostMapping("/addOrEdit")
    @ResponseBody
    public Result<?> addOrEdit(@RequestBody SysTable sysTable) {
        sysTableService.addOrModify(sysTable);
        return Result.OK("ok");
    }


    /**
     * 批量添加
     *
     * @param list
     * @return
     */
    @PostMapping("/saveBatch")
    @ResponseBody
    public Result<?> saveBatch(@RequestBody List<TableDTO> list) {
        String message = sysTableService.saveBatchTable(list);
        if("".equals(message)) {
            return Result.OK("导入成功");
        }else{
            return Result.OK(message);
        }
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @GetMapping("/delete/{id}")
    @ResponseBody
    public Result<?> delete(@PathVariable String id) {
        sysTableService.removeById(id);
        return Result.OK("ok");
    }


}

